<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp"%>
<%@ include file="../common.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script language="javascript" src="<c:url value="../../scripts/prototype/prototype.js"/>"></script>
<%@ include file="/components/calendar/calendar.jsp" %>
<script language="javascript" type="text/javascript" src="<c:url value='/components/datepicker/WdatePicker.js'/>"></script>
<%
	String building = cn.firstsoft.firstframe.plugins.compass.constants.Constants.getRebuilding();
%>

<c:set var="building" value="<%=building%>" />


<html>
  <head>
		<title>管理控制台 - 索引管理</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link href="<c:url value='/admin/style.css'/>" type="text/css" rel="stylesheet">
		<script language="javascript" type="text/javascript" src="<c:url value='/scripts/jquery/jquery-1.6.4.min.js'/>"></script>
<style type="text/css" title="gridStyle">
	.controlColumn {width:40px;height:20px;overflow:hidden}
	.editableTD {}
	html { overflow: scroll; } 
</style>
<link rel="stylesheet" type="text/css" href="<c:url value="/scripts/datatables/css/01/demo_table_jui.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/scripts/datatables/css/01/ColReorder.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/scripts/datatables/css/01/TableTools.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/scripts/datatables/css/01/ColVis.css"/>">
<script type="text/javascript" language="javasript" src="<c:url value='/scripts/jquery/jquery-ui-1.8.21.custom.min.js'/>"></script>
<script type="text/javascript" language="javasript" src="<c:url value='/scripts/json2.js'/>"></script>
<script type="text/javascript" language="javasript" src="<c:url value='/scripts/datatables/js/jquery.dataTables.js'/>"></script>
<script type="text/javascript" language="javasript" src="<c:url value='/scripts/datatables/js/ZeroClipboard.js'/>"></script>
<script type="text/javascript" language="javasript" src="<c:url value='/scripts/datatables/js/TableTools.js'/>"></script>
<script type="text/javascript">
jQuery(document).ready(initTable);

function initTable (flag){
	var oTable = jQuery('#dTGrid_').dataTable({
		"aoColumnDefs":[
            {"aTargets": [0],
            	"mDataProp": null,
            	"sDefaultContent": "管理员重建了索引",
            	"sTitle": '重建索引',
            	"sName": "index_",
            	"sWidth": "10%",
            	"bVisible": true,
            	"bSortable": false},
			{"aTargets": [1],
            	"mDataProp":"rebuiltStartTime",
				"sTitle":"索引开始时间",
				"sName":"rebuiltStartTime",
				"sWidth": "15%",
            	"bVisible": true,
				"bSortable": true},
			{"aTargets": [2],
	            "mDataProp":"rebuiltEndTime",
				"sTitle":"索引结束时间",
				"sName":"rebuiltEndTime",
				"sWidth": "15%",
            	"bVisible": true,
				"bSortable": true},
			{"aTargets": [3],
	            "mDataProp":"succeed",
				"sTitle":"是否全部成功",
				"sName":"succeed",
				"sWidth": "20%",
            	"bVisible": true,
				"bSortable": true},
			{"aTargets": [4],
	            "mDataProp":"consumedTime",
				"sTitle":"耗时",
				"sName":"consumedTime",
				"sWidth": "15%",
            	"bVisible": true,
				"bSortable": true},
			{"aTargets": [5],
	            "mDataProp":"id",
				"sTitle":"日志明细",
				"sName":"other",
				"sWidth": "10%",
            	"bVisible": true,
				"bSortable": true}],
		//"sDom": '<"H"Tlr>t<"F"ip>',
		"sDom": 'R<"dataTables_my_div"r>t<"dataTables_my_div"ip>',
		"bSortClasses": false,//排序列是否使用特定样式
		"oTableTools": {
			"sSwfPath": "<c:url value='/scripts/datatables/swf/copy_cvs_xls_pdf.swf'/>",
			"aButtons": [
				{
					"sExtends":    "collection",
					"sButtonText": "Save",
					"aButtons":    [ "csv", "xls", "pdf" ]
				}
			]
		},
		"sPaginationType": "full_numbers",
		"oLanguage": {
			"sUrl": "<c:url value='/scripts/datatables/i18n/data_tables_cn.txt'/>"
		},
		"aaSorting": [[5,'desc']],
		"bJQueryUI": true,
		"bAutoWidth": false,
		"bProcessing": true,
		"bServerSide": true,
		"sAjaxSource": "<c:url value='/plugins/compass/rebuiltIndexRecord.do?method=jqList'/>",
		"bRetrieve": true,
		"fnServerData":retrieveData
	});
	if(flag == 'true'){
		oTable.fnDraw();   
	}
	lastRebuiltRecord();
};

function retrieveData( sSource, aoData, fnCallback ) {   
	aoData.push({"name":"startTime","value":jQuery("#rebuiltStartTime").val()});
	aoData.push({"name":"endTime","value":jQuery("#rebuiltEndTime").val()});
	aoData.push({"name":"succeed","value":jQuery("#succeed").val()});
	jQuery.post( sSource, aoData, function (json) {
             fnCallback(json) 
       } ,"json");
}  


function restoreDataTables(){
	var oTable = jQuery('#dTGrid_').dataTable();
	oTable.fnDraw();
}
</script>




 </head>
  <body>
  <form name="searchRecord" action="" method="post">
  <table width="100%" align="center" border="0" id="table1">
  		<tr>
			<th align="left" colSpan="4" >
				<font color="#B6A97F">重建索引</font>
			</th>
		</tr>
		<tr>
			<td><font color="#B6A97F">1.升级版本后需要重建索引，可对您的旧版本数据重新建立索引；</font></td>
		</tr>
		<tr>
			<td><font color="#B6A97F">2.索引重建会影响您的使用，请您在下班后进行重建索引的操作。</font></td>
		</tr>
		
		<tr>
			<th class="listViewPaginationTdS1" align="left" colSpan="4">
				重建索引
			</th>
		</tr>
		<tr>
			<td>
				<INPUT name="rebuiltButton" type=button value="重&nbsp;建" onclick="rebuilt();"/>
				<div id="lastTime" style="display: inline;"></div>
			</td>
		</tr>
		
		<tr>
			<td>
				<table border="0">
					<tr>
						<TD align="left" width="8%">重建时间段：&nbsp;</TD>
						<TD class=dataField align=left colspan="3" style="padding-left: 3px"  width="18%">
				    		<input type="text" id="rebuiltStartTime" name="rebuiltStartTime" size="8" readonly> 
				            <img onclick="WdatePicker({el:'rebuiltStartTime',dateFmt:'yyyy-MM-dd',maxDate:'#F{$dp.$D(\'rebuiltEndTime\')}'})"   src="../../components/datepicker/skin/datePicker.gif" width="16" height="22" align="absmiddle"> 
				            &nbsp;&nbsp;至&nbsp;&nbsp;
			            	<input type="text" id="rebuiltEndTime" name="rebuiltEndTime" size="8" readonly>
				            <img onclick="WdatePicker({el:'rebuiltEndTime',dateFmt:'yyyy-MM-dd',minDate:'#F{$dp.$D(\'rebuiltStartTime\')}'})"    src="../../components/datepicker/skin/datePicker.gif" width="16" height="22" align="absmiddle"> 
						</TD>
						<TD  width="8%">是否全部成功：</TD>
						<TD  width="10%">
							<select name = "succeed" id="succeed">
								  <option value="">全部</option>
								  <option value="Y">是</option>
								  <option value="N">否</option>
							</select>
						</TD>
						
						<td width="5%"><INPUT name="search" type=button value="查&nbsp;询" onclick="initTable('true');"/></td>
						<td width="5%"><INPUT name="cancel" type=button value="取&nbsp;消" onclick="resetTable();"/></td>
					</tr>
				</table>
			</td>
		</tr>
  </table>
  </form>
  <table id="dTGrid_"  cellpadding="0" cellspacing="0" border="0" class="display" style="width:100%"></table>
  </body>
</html>
<div id="waitingDiv" style="display: none;"> 
	<img src="../../images/loading2.gif">
</div> 

<script language="javascript">

	/*****************等待中禁用页面功能*****************/
	/**
	 * 遮罩页面
	 */
	function show(){
		var waiting = document.getElementById("waitingDiv"); 
		waiting.style.display = "block"; 
		waiting.style.position = "absolute"; 
		waiting.style.top = "50%"; 
		waiting.style.left = "50%"; 
		waiting.style.marginTop = "-75px"; 
		if(!document.getElementById("bg"))
		{
			bg = document.createElement("div"); 
			bg.setAttribute("id","bg"); 
			bg.style.background = "#000"; 
			bg.style.width = "100%"; 
			
			bg.style.height = document.body.scrollHeight; 
			bg.style.position = "absolute"; 
			bg.style.top = "0"; 
			bg.style.left = "0"; 
			bg.style.zIndex = "500"; 
			bg.style.opacity = "0.3"; 
			bg.style.filter = "Alpha(opacity=30)"; 
			document.body.appendChild(bg);
			//document.body.style.overflow = "hidden"; 
		} 
		else
		{
			bg.style.display = "block"; 
		}
	}
	
	
	
	
	/**
	 * 释放页面
	 * @return
	 */
	function hide(){
		var waiting = document.getElementById("waitingDiv"); 
		var bg = document.getElementById("bg"); 
		if(waiting && bg)
		{
			waiting.style.display = "none"; 
			bg.style.display = "none";
			//document.body.removeChild(bg);
		}
	}

	init();
	function init()
	{
		if('${building}' == 'true' )
		{
			document.all.rebuiltButton.disabled=true;
			show();
			//检查是否已经完成，如果页面被人刷新过该页面接受不到callbak信息，遮罩不会消失
			setInterval(checkIsFinshed,5000); 
		}
		else
		{
			document.all.rebuiltButton.disabled=false;
			hide();
		}		
		
			
	}

	function checkIsFinshed()
	{
		new Ajax.Request(
					'<c:url value="/plugins/compass/rebuiltIndexRecord.do?method=getRebuiltState"/>',
					{
						method: 'post',
						onComplete: getRebuiltStateCallBak,
						onFailure: function() {
							alert("error");
						}
					}
		);
	}
	
	
	function getRebuiltStateCallBak(data)
	{
		if(data.responseText == 'false')
		{
			document.all.rebuiltButton.disabled=false;
			hide();
		}
	}


	function rebuilt()
	{
		show();
		document.all.rebuiltButton.disabled=true;
		new Ajax.Request(
					'<c:url value="/plugins/compass/rebuiltIndexRecord.do?method=saveRebuiltIndex"/>',
					{
						method: 'post',
						onComplete: rebuiltCallBak,
						onFailure: function() {
							hide();
							document.all.rebuiltButton.disabled=false;
							restoreDataTables();
							alert("error");
						}
					}
		);
	}
	function rebuiltCallBak(data)
	{
		hide();
		document.all.rebuiltButton.disabled=false;
		restoreDataTables();
		lastRebuiltRecord();
		alert(data.responseText);
	}
	
	
	//得到最后一次更建索引的信息
	function lastRebuiltRecord()
	{
		new Ajax.Request(
					'<c:url value="/plugins/compass/rebuiltIndexRecord.do?method=showLastTimeRebuiltRecord"/>',
					{
						method: 'post',
						onComplete: getLastTimeRebuiltRecordCallBak,
						onFailure: function() {
							alert("error");
						}
					}
		);
	}
	function getLastTimeRebuiltRecordCallBak(data)
	{
		if(data.responseText)
		{
			var datas = eval('('+data.responseText+')');
			var time = datas.rebuiltStartTime;
			var consumedTimeStr = datas.consumedTimeStr;
			jQuery('#lastTime').html("&nbsp;&nbsp;<font color='red'>您最近一次重建索引的时间是"+time+" ，共耗时"+consumedTimeStr+"</font>");
		}
		else 
		{
			jQuery('#lastTime').html("&nbsp;&nbsp;<font color='red'>您从未重建过索引！ </font>");
		}
		
	}

	//取消
	function resetTable()
	{
		jQuery("#rebuiltStartTime").attr("value","");
		jQuery("#rebuiltEndTime").attr("value","");
		jQuery("#succeed").attr("value","");
		initTable("true");
	}
</script>
